package com.bluetooth.phone;

import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bluetooth.phone.multi.DivideItemDecoration;
import com.bluetooth.phone.optionb.ItemDragHelper;
import com.bluetooth.phone.optionb.OnItemDragListener;
import com.hsae.bluetooth.phones.R;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    RecyclerView subRecyclerView;
    RecyclerView dialogRecyclerView;
    SubAdapter subAdapter;
    DialogAdapter dialogAdapter;
    List<DockItem> contentList = new ArrayList<>();
    List<DockItem> contentLists = new ArrayList<>();
    TextView tv_edit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setDragData();
        dialogAdapter = new DialogAdapter(contentLists);

        subRecyclerView = findViewById(R.id.sub_RecyclerView);
        dialogRecyclerView = findViewById(R.id.dialog_RecyclerView);
        //创建adapter
        subAdapter = new SubAdapter(contentList, this);

        RecyclerView.LayoutManager layoutManager = dialogRecyclerView.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager){
            ((GridLayoutManager)layoutManager).setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    RecyclerView.Adapter adapter = dialogRecyclerView.getAdapter();
                    assert adapter != null;
                    if (adapter.getItemViewType(position) == R.layout.item_dock_air) {
                        return 2;//占1格
                    }
                    return 1;
                }

            });
        }
        RecyclerView.LayoutManager layoutManager2 = subRecyclerView.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager){
            ((GridLayoutManager)layoutManager2).setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    RecyclerView.Adapter adapter = subRecyclerView.getAdapter();
                    assert adapter != null;
                    if (adapter.getItemViewType(position) == R.layout.item_dock_air) {
                        return 2;//占1格
                    }
                    return 1;
                }

            });
        }




        subRecyclerView.setLayoutManager(layoutManager2);
        dialogRecyclerView.setLayoutManager(layoutManager);
        //设置默认的布局方式
        //设置adapter
        dialogRecyclerView.setAdapter(dialogAdapter);
        subRecyclerView.setAdapter(subAdapter);
        //   subRecyclerView.removeItemDecorationAt(0);
        DivideItemDecoration spacing = new DivideItemDecoration(10, 10);
        //  spacing.needVerticalSpacingInSingleLine = false;
        DivideItemDecoration spacing2 = new DivideItemDecoration(10, 10);
        subRecyclerView.addItemDecoration(spacing2);
        dialogRecyclerView.addItemDecoration(spacing);

        // dragListener.setDragListener(subAdapter);

      //  DragListener currentDragListener = new DragListener();
      //  SettingDragItemHelper.setDragListener(currentDragListener);
      //  subRecyclerView.setOnDragListener(currentDragListener);

        //  subRecyclerView.setId(R.id.sub_RecyclerView);

//        subRecyclerView.post(new Runnable() {
//            @Override
//            public void run() {
//
//
//                ((ViewGroup)subRecyclerView.getParent()).post(new Runnable() {
//                    @Override
//                    public void run() {
//                        Rect bounds = new Rect();
//                      // 获取View2占据的矩形区域在其父View（也就是View1）中的相对坐标
//                        subRecyclerView.getHitRect(bounds);
//                      // 计算扩展后的j矩形区域Bounds相对于View1的坐标，left、top、right、bottom分别为View2在各个方向上的扩展范围
//                        bounds.left += 0;
//                        bounds.top -= 1080;
//                        bounds.right += 1080;
//                        bounds.bottom += 1080;
//                        // 创建TouchDelegate，delegateView为View2
//                        TouchDelegate touchDelegate = new TouchDelegate(bounds, subRecyclerView);
//                        // 为View1设置TouchDelegate，原因可以参考View.java中mTouchDelegate的注释
//                        // The delegate to handle touch events that are physically in this view but should be handled by another view.
//                        ((ViewGroup)subRecyclerView.getParent()).setTouchDelegate(touchDelegate);
//                    }
//                });
//            }
//        });
        // TODO 方案2
        dragHelper = new ItemDragHelper(findViewById(R.id.frame_layout));
        dragHelper.addOnItemDragListener(new OnItemDragListener() {
            @Override
            public void onDragFinish(RecyclerView recyclerView, int itemRecyclerPos, int itemPos) {
                String text = String.format("拖动起始第%s个列表的第%s项 结束第%s个列表的第%s项 \n\n拖动数据:%s", originalRecyclerPosition,
                        originalItemPosition, itemRecyclerPos, itemPos, dragItemData);
                Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onDragStart() {
                super.onDragStart();
            }

            @Override
            public float getScale() {
                return super.getScale();
            }
        });
        subAdapter.attachedToDragHelper(dragHelper);

    }

    private ItemDragHelper dragHelper;
    private void setDragData() {
        DockItem dockItem1 = new DockItem("哈哈", 15);
        DockItem dockItem2 = new DockItem("打打", 16);
        DockItem dockItem3 = new DockItem("看看", 17);
     //   dockItem1.setItemType(R.layout.item_dock_air);
        contentList.add(dockItem1);
        contentList.add(dockItem2);
        contentList.add(dockItem3);


        for (int i = 0; i < 15; i++) {
            DockItem dockItem = new DockItem(i + "", i);
            if (i == 2 || i == 14) {
               // dockItem.setItemType(R.layout.item_dock_air);
                dockItem.setItemType(1);
            }
            contentLists.add(dockItem);
        }
    }

}